using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;
using System.Text;
using System.Xml;
using System.IO;

namespace FlashSolution
{
    [Guid("8fce54e0-5008-49f1-b4c4-838bae12f4a8")]
    public class FlashWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {

        /// <summary>
        /// Constructor
        /// </summary>
        public FlashWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// The SWFObjectControl
        /// </summary>
        private SWFObjectControl control = null;

        /// <summary>
        /// Flash-attributes
        /// </summary>
        private FlashAttributes flashattributes = new FlashAttributes();

        public FlashAttributes FlashAttributes
        {
            get
            {
                return flashattributes;
            }
        }

        /// <summary>
        /// Create child controls
        /// </summary>
        protected override void CreateChildControls()
        {
            // Create the SWFObject control
            control = new SWFObjectControl();
            this.Controls.Add(control);

            base.CreateChildControls();
        }

        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editorparts = new List<EditorPart>();

            foreach (EditorPart part in base.CreateEditorParts())
            {
                editorparts.Add(part);
            }

            editorparts.Add(new FlashVarsEditorPart());

            return new EditorPartCollection(editorparts);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (control != null)
            {
                // Set the Flash Attributes
                control.Attributes = flashattributes;

                // Set the AltHtml
                control.AltHtml = AltHtml;

                control.ximovie = XIMovieUrl;

                control.FlashContainerTag = FlashContainerTag;

                control.ContainerCssClass = ContainerCssClass;

                control.FlashCssClass = FlashCssClass;
            }
            base.OnPreRender(e);
        }

        #region Public WebPart Properties
        /// <summary>
        /// Alternative Html
        /// </summary>
        [WebBrowsable(true),Category("Flash Container"),WebDisplayName("Alternative Html"),WebDescription("Alternative Html"),Personalizable(PersonalizationScope.Shared)]
        public string AltHtml
        {
            get;
            set;
        }

        /// <summary>
        /// URL of the ExpressInstall flash movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Version"),WebDisplayName("ExpressInstall Movie url"),WebDescription("URL of the ExpressInstall flash movie"),Personalizable(PersonalizationScope.Shared)]
        public string XIMovieUrl
        {
            get;
            set;
        }

        /// <summary>
        /// Flash Container Tag
        /// </summary>
        [WebBrowsable(true),Category("Flash Container"),WebDisplayName("Flash Container Tag"),WebDescription("Flash Container Tag"),Personalizable(PersonalizationScope.Shared)]
        public SWFObjectControl.enumFlashContainerTag FlashContainerTag
        {
            get;
            set;
        }

        /// <summary>
        /// Flash Container Class
        /// </summary>
        [WebBrowsable(true),Category("Flash Container"),WebDisplayName("Container CssClass"),WebDescription("Stylesheet class for Flash Container"),Personalizable(PersonalizationScope.Shared)]
        public string ContainerCssClass
        {
            get;
            set;
        }

        /// <summary>
        /// Flash Class
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Flash CssClass"),WebDescription("Stylesheet class for Flash"),Personalizable(PersonalizationScope.Shared)]
        public string FlashCssClass
        {
            get;
            set;
        }

        #endregion

        #region Flash Attributes Public Fields
        /// <summary>
        /// URL of the flash movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Movie url"),WebDescription("URL of the flash movie"),Personalizable(PersonalizationScope.Shared)]
        public string MovieUrl
        {
            get { return flashattributes.movie; }
            set { flashattributes.movie = value; }
        }

        /// <summary>
        /// Width of the movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Movie width"),WebDescription("Width of the movie."),Personalizable(PersonalizationScope.Shared)]
        public string MovieWidth
        {
            get { return flashattributes.width; }
            set { flashattributes.width = value; }
        }

        /// <summary>
        /// Height of the movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Movie height"),WebDescription("Height of the movie."),Personalizable(PersonalizationScope.Shared)]
        public string MovieHeight
        {
            get { return flashattributes.height; }
            set { flashattributes.height = value; }
        }

        /// <summary>
        /// Required FlashPlayer version. 
        /// </summary>
        [WebBrowsable(true),Category("Flash Version"),WebDisplayName("Required Minimal FlashPlayer Version"),WebDescription("Required Minimal FlashPlayer version."),Personalizable(PersonalizationScope.Shared)]
        public string Version
        {
            get { return flashattributes.version; }
            set { flashattributes.version = value; }
        }

        /// <summary>
        /// Identifies the Flash movie to the host environment (a web browser, for example) so that it can be referenced using a scripting language. OBJECT-specific. 
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("MovieID"),WebDescription("Identifies the Flash movie to the host environment (a web browser, for example) so that it can be referenced using a scripting language. OBJECT-specific."),Personalizable(PersonalizationScope.Shared)]
        public string MovieID
        {
            get { return flashattributes.id; }
            set { flashattributes.id = value; }
        }

        /// <summary>
        /// Identifies the Flash movie to the host environment (a web browser, typically) so that it can be referenced using a scripting language such as JavaScript or VBScript. EMBED-specific. 
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Name"),WebDescription("Identifies the Flash movie to the host environment (a web browser, typically) so that it can be referenced using a scripting language such as JavaScript or VBScript."),Personalizable(PersonalizationScope.Shared)]
        public string Name
        {
            get { return flashattributes.name; }
            set { flashattributes.name = value; }
        }

        /// <summary>
        /// Specifies whether the browser should start Java when loading the Flash Player for the first time. The default value is false if this attribute is omitted. If you use JavaScript and Flash on the same page, Java must be running for the FSCommand to work. 
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("SWLiveConnect"),WebDescription("Specifies whether the browser should start Java when loading the Flash Player for the first time."),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean SWLiveCconnect
        {
            get { return flashattributes.swliveconnect; }
            set { flashattributes.swliveconnect = value; }
        }

        /// <summary>
        /// Specifies whether the movie begins playing immediately on loading in the browser. The default value is true if this attribute is omitted. 
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Play"),WebDescription("Specifies whether the movie begins playing immediately on loading in the browser."),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean Play
        {
            get { return flashattributes.play; }
            set { flashattributes.play = value; }
        }

        /// <summary>
        /// Specifies whether the movie repeats indefinitely or stops when it reaches the last frame. The default value is true if this attribute is omitted. 
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Loop"),WebDescription("Specifies whether the movie repeats indefinitely or stops when it reaches the last frame."),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean Loop
        {
            get { return flashattributes.loop; }
            set { flashattributes.loop = value; }
        }

        /// <summary>
        /// Setting to choose wether to displays the full menu, allowing the user a variety of options to enhance or control playback. Default is false.
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Menu"),WebDescription("Setting to choose wether to displays the full menu, allowing the user a variety of options to enhance or control playback."),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean Menu
        {
            get { return flashattributes.menu; }
            set { flashattributes.menu = value; }
        }

        /// <summary>
        /// Playback quality of the movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Quality"),WebDescription("Playback quality of the movie"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumQuality Quality
        {
            get { return flashattributes.quality; }
            set { flashattributes.quality = value; }
        }

        /// <summary>
        /// Scaling of the movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Scale"),WebDescription("Scaling of the movie"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumScale Scale
        {
            get { return flashattributes.scale; }
            set { flashattributes.scale = value; }
        }

        /// <summary>
        /// Alignment of the movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Align"),WebDescription("Alignment of the movie"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumAlign Align
        {
            get { return flashattributes.align; }
            set { flashattributes.align = value; }
        }

        /// <summary>
        /// Alignment of the movie
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("sAlign"),WebDescription("Alignment of the movie"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumSAlign sAlign
        {
            get { return flashattributes.salign; }
            set { flashattributes.salign = value; }
        }

        /// <summary>
        /// WindowMode setting
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("WindowMode"),WebDescription("WindowMode setting"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumWMode WindowMode
        {
            get { return flashattributes.wmode; }
            set { flashattributes.wmode = value; }
        }

        /// <summary>
        /// Background color setting. #FFFFFF or ffffff
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Background color (format: #FFFFFF)"),WebDescription("Background color setting. #FFFFFF or ffffff"),Personalizable(PersonalizationScope.Shared)]
        public string BackgroundColor
        {
            get { return flashattributes.bgcolor; }
            set { flashattributes.bgcolor = value; }
        }

        /// <summary>
        /// sets the base directlory or URL to use to resolve relative paths
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Basepath"),WebDescription("sets the base directlory or URL to use to resolve relative paths"),Personalizable(PersonalizationScope.Shared)]
        public string Basepath
        {
            get { return flashattributes.basepath; }
            set { flashattributes.basepath = value; }
        }

        /// <summary>
        /// FlashVars
        /// </summary>
        [WebBrowsable(false),Category("Flash Settings"),WebDisplayName("FlashVars"),WebDescription("FlashVars collection"),Personalizable(PersonalizationScope.Shared)]
        public string flashvars
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair<string, string> var in flashattributes.flashvars)
                {
                    sb.AppendFormat("{0}={1}\r\n", var.Key, var.Value);
                }
                return sb.ToString();
            }
            set
            {
                flashattributes.flashvars.Clear();

                if (!string.IsNullOrEmpty(value))
                {
                    StringReader sr = new StringReader(value);
                    string line = sr.ReadLine();
                    while (line != null)
                    {
                        string[] ss = line.Split(new char[] { '=' }, 2);
                        if (ss.Length == 2)
                        {
                            string key = ss[0].Trim();
                            string val = ss[1].Trim();
                            flashattributes.flashvars.Add( key , val );
                        }
                        line = sr.ReadLine();
                    }
                    sr.Close();
                }
            }
        }

        /// <summary>
        /// use a device font for a static text field.
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("DeviceFont"),WebDescription("use a device font for a static text field."),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean DeviceFont
        {
            get { return flashattributes.devicefont; }
            set { flashattributes.devicefont = value; }
        }

        /// <summary>
        /// The AllowScriptAccess parameter of the Macromedia Flash Player controls the ability to perform outbound scripting from within a Flash SWF.
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("AllowsSriptAccess"),WebDescription("The AllowScriptAccess parameter of the Macromedia Flash Player controls the ability to perform outbound scripting from within a Flash SWF."),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumAllowScriptAccess AllowScriptAccess
        {
            get { return flashattributes.allowscriptaccess; }
            set { flashattributes.allowscriptaccess = value; }
        }

        /// <summary>
        /// Extra params to specify height in percentage (i.e. 100%)
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("sHeight"),WebDescription("Extra params to specify height in percentage (i.e. 100%)"),Personalizable(PersonalizationScope.Shared)]
        public string sHeight
        {
            get { return flashattributes.sHeight; }
            set { flashattributes.sHeight = value; }
        }

        /// <summary>
        /// Extra params to specify width in percentage (i.e. 100%)
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("sWidth"),WebDescription("Extra params to specify width in percentage (i.e. 100%)"),Personalizable(PersonalizationScope.Shared)]
        public string sWidth
        {
            get { return flashattributes.sWidth; }
            set { flashattributes.sWidth = value; }
        }


        /// <summary>
        /// AllowFullscreen
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Allow Fullscreen"),WebDescription("Allow Fullscreen"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean AllowFullscreen
        {
            get { return flashattributes.allowfullscreen; }
            set { flashattributes.allowfullscreen = value; }
        }

        /// <summary>
        /// AllowFullscreen
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("SeamlessTabbing"),WebDescription("SeamlessTabbing"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumBoolean SeamlessTabbing
        {
            get { return flashattributes.seamlesstabbing; }
            set { flashattributes.seamlesstabbing = value; }
        }

        /// <summary>
        /// Allow Networking
        /// </summary>
        [WebBrowsable(true),Category("Flash Settings"),WebDisplayName("Allow Networking"),WebDescription("Allow Networking"),Personalizable(PersonalizationScope.Shared)]
        public FlashAttributes.enumNetworking AllowNetworking
        {
            get { return flashattributes.allownetworking; }
            set { flashattributes.allownetworking = value; }
        }

        #endregion
    }
}
